//
//  APPResponse.swift
//  B2BAutoMall
//
//  Created by heew on 16/1/28.
//  Copyright © 2016年 qeegoo. All rights reserved.
//

import UIKit
import SwiftyJSON

// 公用key
let kResponse_status      = "status"
let kResponse_status_code = "code"
let kResponse_status_msg  = "msg"
let kResponse_data        = "data"
let kResponse_list        = "list"
let kResponse_pageNo      = "pageNo"
let kResponse_totalPages  = "totalPages"

open class AppResponse: NSObject {
    
    // 存储属性
    var statusCode:String
    var statusMsg:String
    var pageInfo:AppPageInfo
    var jsonData:JSON
    
    
    init(data : Data?) {
//        super.init()
        let json = JSON(data: data ?? Data())
        statusCode = json[kResponse_status][kResponse_status_code].stringValue
        statusMsg = json[kResponse_status][kResponse_status_msg].stringValue
        pageInfo = AppPageInfo(json: json[kResponse_data])
        jsonData = json[kResponse_data]
        
    }
    
    
    
    // 计算属性
    var isSuccess: Bool {
        return statusCode == "0000"
    }
    
    var isLoginTokenInvalidation: Bool {
        return statusCode == "0009"
    }
    
//    class func responseWithResponseDictionary(dictResponse : NSDictionary) -> AppResponse {
//        let response = AppResponse()
//        response.dictResponse = dictResponse
//        if let status = dictResponse[kResponse_status] as? NSDictionary {
//            response.statusCode = status.stringForKey(kResponse_status_code as NSString!) as String
//            response.statusMsg = status.stringForKey(kResponse_status_msg as NSString!) as String
//            response.jsonData = JSON(dictionaryLiteral: (" ", "没有数据"))
//        }
//        if let data = dictResponse.object(forKey: kResponse_data) as? NSDictionary{
//            let pageInfo = AppPageInfo()
//            response.data = data
//            pageInfo.pageNo = data.stringForKey(kResponse_pageNo as NSString!).integerValue
//            pageInfo.totalPages = data.stringForKey(kResponse_totalPages as NSString!).integerValue
//            response.pageInfo = pageInfo
//            response.jsonData = JSON(data)
//        }
//        return response
//    }
//    
}
